<template>
  <div>
    <div>
      <van-sticky>
        <el-card shadow="nover" class="mobile-card-search">
          <el-row :gutter="20">
            <el-col :span="20">
              <el-input
                placeholder="请输入任务名称"
                prefix-icon="el-icon-search"
                v-model="queryParams.nodeName"
                @keyup.enter.native="handleQuery"
              ></el-input>
            </el-col>
            <el-col :span="4">
              <i
                :style="{ color: colorFlag ? '#1890ff' : '' }"
                class="el-icon-s-operation search-icon"
                @click="showPicker = true"
              ></i>

              <!-- 搜索弹窗 -->
              <van-popup
                v-model="showPicker"
                position="bottom"
                round
                style="height: 80%"
                close-icon="close"
                closeable
                @close="popupClose"
                class="modal-content"
              >
                <h3 style="text-align: center">筛选</h3>
                <van-form class="modal-body" ref="queryForm">
                  <searchable-picker
                    v-model="queryParams.flowStatus"
                    :columns="dict.type.flow_status"
                    valueKey="value"
                    labelKey="label"
                    label="流程状态"
                    placeholder="点击选择流程状态"
                  />
                </van-form>
                <div style="" class="modal-footer">
                  <van-button
                    type="default"
                    style="width: 40%; binstance-radius: 10px"
                    @click="resetQuery"
                    >重置</van-button
                  >
                  <van-button
                    type="info"
                    style="width: 40%; binstance-radius: 10px"
                    @click="handleQuery"
                    >查询</van-button
                  >
                </div>
              </van-popup>
              <!-- 搜索弹窗 -->
            </el-col>
          </el-row>
        </el-card>
      </van-sticky>
    </div>
    <div class="mobile-container">
      <van-list
        v-model="loading"
        :finished="finished"
        @load="getList"
        finished-text="没有更多了...."
      >
        <div v-if="instanceList.length > 0">
          <el-card
            class="mobile-card"
            shadow="hover"
            v-for="instance in instanceList"
            :key="instance.instanceId"
          >
            <div slot="header">
              <span class="mobile-card-titel">{{ instance.flowName }}</span>
              <van-popover
                placement="bottom-end"
                style="float: right"
                v-model="instance.flag"
                trigger="click"
                :actions="actions"
                @select="onSelect($event, instance)"
                @closed="
                  () => {
                    instance.flag = false;
                  }
                "
              >
                <template #reference>
                  <i
                    class="el-icon-more"
                    @click.stop="instance.flag = true"
                  ></i>
                </template>
              </van-popover>
            </div>
            <div style="color: gray; font-size: small">发起人:</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="instance.submitUser">{{ instance.submitUser }}</div>
              <div v-else>--</div>
            </div>
            <div style="color: gray; font-size: small">当前节点:</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="instance.nodeName">{{ instance.nodeName }}</div>
              <div v-else>--</div>
            </div>

            <div v-if="instance.businessValue" class="text">
              <div v-for="(value, key) in instance.businessValue" :key="key">
                <div style="color: gray; font-size: small">{{ key }}：</div>
                <div style="margin: 10px 0; font-size: medium">
                  <div v-if="value">{{ value }}</div>
                  <div v-else>--</div>
                </div>
              </div>
            </div>
            <div style="color: gray; font-size: small">发起时间：</div>
            <div style="margin: 10px 0; font-size: medium">
              <div v-if="instance.submitTime">
                {{ parseTime(instance.submitTime, "{y}-{m}-{d}") }}
              </div>
              <div v-else>--</div>
            </div>

            <div style="color: gray; font-size: small">流程状态:</div>
            <div style="margin: 10px 0; font-size: medium">
              <dict-tag
                :options="dict.type.flow_status"
                :value="instance.flowStatus"
                v-if="instance.flowStatus"
              />
              <div v-else>--</div>
            </div>
          </el-card>
        </div>
        <van-empty
          description="暂无数据"
          v-if="instanceList.length === 0 && !loading"
        />
      </van-list>
    </div>
  </div>
</template>
<script>
import { toDoPage } from "@/api/flow/execute";
export default {
  name: "Todo",
  dicts: ["flow_status"],
  data() {
    return {
      actions: [{ text: "办理审批", icon: "guide-o" }],
      // 部门名称
      deptName: undefined,
      showPicker: false,
      // 遮罩层
      loading: false,
      finished: false,
      instanceList: [],
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nodeName: null,
        flowStatus: null,
        approver: null,
        createTime: null,
      },
      colorFlag: false,
      flag: false,
    };
  },
  mounted() {},
  methods: {
    onSelect(action, instance) {
      this.handle(instance);
    },
    handle(row) {
      this.taskId = row.id;
      this.businessId = row.businessId;
      if (row.fromCustom == "N" && row.fromPath) {
        this.$router.push({
          path: "/" + row.fromPath,
          query: {
            taskId: this.taskId,
            value: row.businessId,
          },
        });
      }

      if (row.fromCustom == "N" && row.fromPath) {
        // 实际情况是，不同条件对应不同的页面，所以用动态组件
        this.approve = (resolve) =>
          require([`@/views/${row.fromPath}`], resolve);
      }
    },
    popupClose() {
      if (this.queryParams.isDispose) {
        this.colorFlag = true;
      } else {
        this.colorFlag = false;
      }
    },
    /** 查询项目列表 */
    async getList() {
      this.loading = true;
      try {
        let obj = JSON.parse(JSON.stringify(this.queryParams));
        let res = await toDoPage(obj);
        // 判断是否加载完所有数据
        if (this.instanceList.length >= res.total) {
          this.finished = true;
        } else {
          this.instanceList.push(
            ...res.rows.map((item) => ({
              ...item,
              flag: false,
            }))
          );

          this.queryParams.pageNum += 1;
          this.$forceUpdate();
        }
      } catch (error) {
        console.error("加载数据失败", error);
      } finally {
        this.loading = false;
      }
    },
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
      };
      this.handleQuery();
      this.colorFlag = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.instanceList = [];
      this.queryParams.pageNum = 1;
      this.showPicker = false;
      this.getList();
    },
  },
};
</script>

<style scoped>
.modal-content {
  display: table;
  width: 100%;
  height: 100%;
}

.modal-body {
  display: table-row;
  height: 100%;
}

.modal-footer {
  display: table-row;
  text-align: center;
  padding: 16px;
  background-color: white;
  display: flex;
  justify-content: space-around;
  binstance-top: 1px solid #e4dfdf;
}

.modal-footer button {
  width: 45%;
  binstance-radius: 10px;
}

.submitButton {
  position: fixed;
  top: 85%;
  left: 85%;
}
</style>